package com.gpframework.module.wages.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.CustomException;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.wages.domain.GzEmployeeEvaluation;
import com.gpframework.module.wages.domain.GzPersonnelFoundation;
import com.gpframework.module.wages.domain.vo.GzEmployeeEvaluationVo;
import com.gpframework.module.wages.mapper.GzEmployeeEvaluationMapper;
import com.gpframework.module.wages.service.IGzEmployeeEvaluationService;
import com.gpframework.module.wages.service.IGzPersonnelFoundationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * @ClassName GzEmployeeEvaluation
 * @Author gpframework
 * @Date 2024-01-19
 * @Version 1.0.0
 * @Description 员工评定Service业务层处理
 */
@Service
public class GzEmployeeEvaluationServiceImpl extends ServiceImpl<GzEmployeeEvaluationMapper, GzEmployeeEvaluation> implements IGzEmployeeEvaluationService {

    @Autowired
    private IGzPersonnelFoundationService iGzPersonnelFoundationService;
    @Autowired
    private IGzEmployeeEvaluationService iGzEmployeeEvaluationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-19
     * @Param [lqw]
     * @Return List<GzEmployeeEvaluationVo>
     */
    @Override
    public List<GzEmployeeEvaluationVo> listVo(QueryWrapper<GzEmployeeEvaluation> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-19
     * @Param [id]
     * @Return GzEmployeeEvaluationVo
     */
    @Override
    public GzEmployeeEvaluationVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public void deleteByMonth(Date yearMonth) {
        this.baseMapper.deleteByMonth(yearMonth);
    }

    @Override
    public String importListNote(List<GzEmployeeEvaluationVo> excelList, boolean updateSupport) {
        if (StringUtils.isNull(excelList) || excelList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (GzEmployeeEvaluationVo gzEmployeeEvaluationVo : excelList) {
            try {
                //查询数据
                GzEmployeeEvaluation employeeEvaluation = iGzEmployeeEvaluationService.getOne(new QueryWrapper<GzEmployeeEvaluation>()
                        .eq("name", gzEmployeeEvaluationVo.getName())
                        .eq("department_name", gzEmployeeEvaluationVo.getDepartmentName())
                        .eq("month", gzEmployeeEvaluationVo.getMonth())
                        .eq("del_status", YesNoEnum.NO.getKey()));
                if (StringUtils.isNull(employeeEvaluation)){
                    //新增员工评定
                    GzEmployeeEvaluation gzEmployeeEvaluation = Convert.convert(new TypeReference<GzEmployeeEvaluation>() {}, gzEmployeeEvaluationVo);

                    //复制userId
                    GzPersonnelFoundation gzPersonnelFoundation = iGzPersonnelFoundationService.getOne(new QueryWrapper<GzPersonnelFoundation>()
                            .eq("name", gzEmployeeEvaluationVo.getName())
                            .eq("department_name", gzEmployeeEvaluationVo.getDepartmentName())
                            .eq("del_status", YesNoEnum.NO.getKey()));

                    gzEmployeeEvaluation.setUserId(gzPersonnelFoundation.getId());
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + gzEmployeeEvaluation.getName() + " 导入成功");
                    iGzEmployeeEvaluationService.save(gzEmployeeEvaluation);
                } else if (updateSupport) {
                    //更新考勤
                    GzEmployeeEvaluation gzEmployeeEvaluation = Convert.convert(new TypeReference<GzEmployeeEvaluation>() {}, gzEmployeeEvaluationVo);

                    GzEmployeeEvaluation evaluation = iGzEmployeeEvaluationService.getOne(new QueryWrapper<GzEmployeeEvaluation>()
                            .eq("name", gzEmployeeEvaluationVo.getName())
                            .eq("department_name", gzEmployeeEvaluationVo.getDepartmentName())
                            .eq("month", gzEmployeeEvaluationVo.getMonth())
                            .eq("del_status", YesNoEnum.NO.getKey()));
                    if (evaluation!=null){
                        //获取ID
                        gzEmployeeEvaluation.setId(evaluation.getId());
                    }

                    //复制userId
                    GzPersonnelFoundation gzPersonnelFoundation = iGzPersonnelFoundationService.getOne(new QueryWrapper<GzPersonnelFoundation>()
                            .eq("name", gzEmployeeEvaluationVo.getName())
                            .eq("department_name", gzEmployeeEvaluationVo.getDepartmentName())
                            .eq("del_status", YesNoEnum.NO.getKey()));
                    gzEmployeeEvaluation.setUserId(gzPersonnelFoundation.getId());
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + gzEmployeeEvaluation.getName() + " 更新成功");
                    iGzEmployeeEvaluationService.updateById(gzEmployeeEvaluation);
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、姓名 " + gzEmployeeEvaluationVo.getName() + " 已存在");
                }

            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + gzEmployeeEvaluationVo.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }

    @Override
    public List<GzEmployeeEvaluationVo> listVoed(QueryWrapper<GzEmployeeEvaluation> lqw) {
        return this.baseMapper.listVoed(lqw);
    }
}
